<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.code" placeholder="终端编号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.versionId" placeholder="版本号" clearable class="filter-item" style="width: 130px">
        <el-option label="全部" value="" />
        <el-option v-for="item in versionList" :key="item.Id" :label="item.Version" :value="item.Id" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleVersions">
        版本纬度
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="设备编号" align="center">
        <template slot-scope="{row}">
          <span>{{ row.Code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前版本" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.Version }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最新版本" width="150px" align="center">
        <template>
          <span>{{ NewVersion }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.Dt }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import * as homeHttp from '@/api/home'

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '在线': 'success',
        '离线': 'info'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        status: undefined,
        name: undefined
      },
      importanceOptions: [1, 2, 3],
      versionList: [],
      areaList: [{ Name: 'Name', Id: 'Id' }],
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      temp: {
        AreaId: undefined,
        Code: undefined,
        Manufactor: undefined,
        Mac: undefined,
        Ip: undefined,
        Addr: undefined,
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改设备',
        create: 'Create'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false,
      NewVersion: undefined
    }
  },
  created() {
    this.getList()
    this.getPackageVersions()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const res = await homeHttp.GetAppHistoryFromDevice(this.listQuery)

      if (res.data.Result) {
        this.list = res.data.Data
        this.NewVersion = res.data.E
        this.total = res.data.Total
      }

      this.listLoading = false
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleVersions() {
      this.$router.push({ path: '/version/version' })
    },

    async getPackageVersions() {
      const res = await homeHttp.getPackageVersions()

      this.versionList = res.data.Data
    }
  }
}
</script>
